package cn.winwintech.Incense.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FormatUtil {

	public static String formatDate(Date date) {
		DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(date);
	}

	public static String formatDateTime(Date date) {
		DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		return sdf.format(date);
	}

	public static String formatDateTimeS(Date date) {
		DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(date);
	}

	public static String formatDateTimeWithMiliSecond(Date date) {
		DateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		return sdf.format(date);
	}

	public static String timestampToString(Timestamp tt) {
		return formatDateTimeS(new Date(tt.getTime()));
	}

	public static String DatefromLong(long time) {
		return formatDateTimeS(new Date(time));

	}

	public static Timestamp dateTimeFromString(String s) {
		Date date = new Date();// 获取当前时间
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String str = sdf.format(date);// 时间存储为字符串
		Timestamp time = Timestamp.valueOf(str);// 转换时间字符串为Timestamp
		System.out.println(time);// 输出结果
		return time;
	}

	public static Timestamp dateFromString(String s) {
		Date date = new Date();// 获取当前时间
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String str = sdf.format(date);// 时间存储为字符串
		Timestamp time = Timestamp.valueOf(str);// 转换时间字符串为Timestamp
		System.out.println(time);// 输出结果
		return time;
	}

	public static String formateCode(long id1, long id2) {
		return formatDateTimeWithMiliSecond(new Date()) + strFrom(id1, 5) + strFrom(id2, 5);
	}

	public static String strFrom(long code, int width) {
		String str = "" + code;
		while (str.length() < width) {
			str = "0" + str;
		}
		if (str.length() > width) {
			str = str.substring(str.length() - width);
		}
		return str;
	}

}
